package LeetCode.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 存在重复元素2
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/27
 * @time: 13:22
 */
public class Test219存在重复元素II {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
                return true;
            }
            map.put(nums[i], i);
        }
        return false;
    }

}
